package app.fugema.shop.main.center.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.base.common.model.http.jackSon.JacksonUtils;
import com.base.common.utils.RxTimer;
import com.base.common.utils.TimeUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.ada.SingleRecyclerAdapter;
import com.base.common.view.base.BaseFragment;
import com.base.common.viewmodel.BaseViewModel;

import androidx.core.content.ContextCompat;
import app.fugema.shop.databinding.ItemCouponListViewBinding;
import app.fugema.shop.databinding.OrderListFmBinding;

import app.fugema.shop.R;
import app.fugema.shop.main.center.model.CouponListResult;
import app.fugema.shop.main.goods.view.ShopActivity;

import java.util.List;

import androidx.databinding.DataBindingUtil;

public class CouponListFragment extends BaseFragment<OrderListFmBinding, BaseViewModel>
{
    private static String COUPON_TAG = "CouponListFragment";

    private List<CouponListResult.CouponBean> couponList;

    public static CouponListFragment getInstance(List<CouponListResult.CouponBean> couponBeanList)
    {
        Bundle args = new Bundle();
        String jsonList = JacksonUtils.transBean2Json( couponBeanList );
        args.putString(COUPON_TAG, jsonList);
        CouponListFragment fragment = new CouponListFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected OrderListFmBinding initDataBinding(LayoutInflater inflater, ViewGroup container)
    {
        return DataBindingUtil.inflate(inflater, R.layout.order_list_fm, container, false);
    }


    private SingleRecyclerAdapter<CouponListResult.CouponBean, ItemCouponListViewBinding> couponListAdapter;
    private RxTimer rxTimer;

    @Override
    public void initView()
    {
        super.initView();

        couponListAdapter = new SingleRecyclerAdapter<CouponListResult.CouponBean,ItemCouponListViewBinding>()
        {
            @Override
            public int getLayoutId()
            {
                return R.layout.item_coupon_list_view;
            }

            @Override
            public void onBindViewHolder(ItemCouponListViewBinding binding, int position, CouponListResult.CouponBean bean)
            {

                //0=未使用，1=已使用
                if( bean.getUseStatus() == 0 )
                {
                    Long nowTime = System.currentTimeMillis();
                    // 当前时间 大于等于 优惠券结束时间   为过期优惠券   2021/5/22 0:00:00
                    if( nowTime >= TimeUtil.getMilliTimeByDateStr(bean.getEndTime(),"yyyy/MM/dd HH:mm:ss"))
                    {
                        binding.btnUse.setText("已过期");
                        binding.btnUse.setEnabled(false);
                        binding.btnUse.setTextColor(ContextCompat.getColor(getContext(),R.color.C_999999));
                        binding.btnUse.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(),R.color.C_DDDDDD));
                    }
                    else
                    {
                        binding.btnUse.setText("去使用");
                        binding.btnUse.setEnabled(true);
                        binding.btnUse.setTextColor(ContextCompat.getColor(getContext(),R.color.C_FFFFFF));
                        binding.btnUse.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(),R.color.redColor));
                    }
                }
                else
                {
                    binding.btnUse.setText("已使用");
                    binding.btnUse.setEnabled(false);
                    binding.btnUse.setTextColor(ContextCompat.getColor(getContext(),R.color.C_999999));
                    binding.btnUse.getDelegate().setBackgroundColor(ContextCompat.getColor(getContext(),R.color.C_DDDDDD));
                }

            }

            @Override
            public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, CouponListResult.CouponBean bean)
            {
                super.onItemClick(view, adapter, onclickType, position, bean);
                ShopActivity.start(getActivity(),bean.getVShop().getVShopId());
            }
        };
        binding.rvRecyclerView.setAdapter(couponListAdapter);

        if (rxTimer == null) {
            rxTimer = new RxTimer();
        } else {
            rxTimer.cancel();
        }
        /*
        // 定时器  每隔1秒 执行  一直执行
        rxTimer.interval(1000, new RxTimer.RxAction() {
            @Override
            public void action(long number)
            {
                LogUtil.e("rxTimer  number time  -->  " + number  );
            }
        });
        */
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (rxTimer != null)
        {
            rxTimer.cancel();
        }
    }

    @Override
    public void initData()
    {
        super.initData();

        String listJson = getArguments().getString(COUPON_TAG,"");
        if( UIUtils.isNotEmpty(listJson) )
        {
            couponList = JacksonUtils.getConverList(listJson,CouponListResult.CouponBean.class);
        }
        if( UIUtils.isNotEmpty( couponList ) )
        {
            couponListAdapter.setDataList( couponList );
        }
        else
        {

        }
        //viewUtils.setRefreshStateLayout(binding.rvRecyclerView, this, this);
        //viewUtils.setDataListRefreshLayout(bookingListAdapter, data.data.getData().getCurrent(), data.data.getData().getSize(), data.data.getData().getRecords());


    }
}
